from PyQt5.Qt import *

class MyQWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("父子对象")

        """
        设置父对象，父对象只能设置一个
        <PyQt5.QtCore.QObject object at 0x00000000030DB048>
        <PyQt5.QtCore.QObject object at 0x00000000030DB0D8>
        <PyQt5.QtCore.QObject object at 0x00000000030DB048>
        """
        obj1 = QObject()
        obj2 = QObject()
        obj3 = QObject()
        obj4 = QObject()
        obj4.setObjectName("4")
        obj5 = QObject()
        obj6 = QObject()
        obj2.setParent(obj1)
        obj3.setParent(obj1)
        obj4.setParent(obj2)
        obj5.setParent(obj3)
        obj6.setParent(obj3)
        #print(obj1)
        #print(obj2)
        #print(obj2.parent()) # 获取父对象

        """
        获取所有子对象
        # [<PyQt5.QtCore.QObject object at 0x0000000002CCC0D8>, <PyQt5.QtCore.QObject object at 0x0000000002CCC168>]
        """
        #print(obj1.children())

        """
        查找子对象
        findChild(参数1, 参数2, 参数3) : 查询一个
        findChildren(参数1, 参数2, 参数3): 查询多个
        参数1: 类型, 例如QObject
        参数2: 对象名称, 可以省略
        参数3: 查找选项, FindChildrenRecursively(递归查找，默认选项，可以省略), FindDirectChildrenOnly(只查找直接子对象)
        """
        obj1.findChild(QObject, "4", Qt.FindDirectChildrenOnly)
        #print(o)
        os = obj1.findChildren(QObject)
        print(os)

# 测试
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MyQWidget()
    window.show()
    sys.exit(app.exec_())